package com.tanhua.server.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.pojo.BlackList;
import com.tanhua.common.pojo.Settings;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.server.vo.BlackListVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.SettingsVo;
import com.tanhua.server.vo.UserInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 小霞
 * @version 1.0
 * @description: 根据用户id查询个人信息
 * @date 2021/7/6 19:13
 */


@Service
public class MyCenterService {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private BlackListService blackListService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private TanHuaService tanHuaService;

    public UserInfoVo queryUserInfoByUserId(Long userId) {
        if (ObjectUtil.isEmpty(userId)) {
            //如果查询id为null，就表示查询当前用户信息
            userId = UserThreadLocal.get().getId();
        }
        //查询用户信息
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(userId);
        if (ObjectUtil.isEmpty(userInfo)) {
            return null;
        }

        UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class, "marriage");
        userInfoVo.setGender(userInfo.getSex().getValue() == 1 ? "man" : "women");
        userInfoVo.setMarriage(StrUtil.equals("已婚", userInfo.getMarriage()) ? 1 : 0);
        return userInfoVo;
    }


    /**
     * 修改用户信息
     *
     * @param userInfoVo
     * @return
     */
    public Boolean updateUserInfo(UserInfoVo userInfoVo) {
        //获取个人用户信息
        User user = UserThreadLocal.get();
        UserInfo userInfo = new UserInfo();

        //修改个人信息
        userInfo.setUserId(user.getId());
        userInfo.setNickName(userInfoVo.getNickname());
        userInfo.setBirthday(userInfoVo.getBirthday());
        userInfo.setSex(StringUtils.equalsIgnoreCase(userInfoVo.getGender(), "man") ? SexEnum.MAN : SexEnum.WOMAN);
        userInfo.setCity(userInfoVo.getCity());
        userInfo.setEdu(userInfoVo.getEducation());
        userInfo.setIncome(userInfoVo.getIncome());
        userInfo.setIndustry(userInfoVo.getProfession());
        userInfo.setMarriage(userInfoVo.getMarriage() == 1 ? "已婚" : "未婚");

        return this.userInfoService.queryUpdate(userInfo);
    }

    /**
     * 黑名单查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult querBlacklistService(Integer page, Integer pageSize) {
        //拿到用户信息
        User user = UserThreadLocal.get();
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        //查询用户
        IPage<BlackList> blackListIPage = this.blackListService.queryBlacklist(user.getId(), page, pageSize);
        //总记录数
        pageResult.setCounts(Convert.toInt(blackListIPage.getTotal()));
        pageResult.setPages(Convert.toInt(blackListIPage.getPages()));

        //获取列表数据
        List<BlackList> records = blackListIPage.getRecords();
        if (CollUtil.isEmpty(records)) {
            return pageResult;
        }
        //获取黑名单用户id,然后通过id去查询用户信息，遍历封装
        List<Object> blackUserIds = CollUtil.getFieldValues(records, "blackUserId");

        //获取到黑名单用户信信
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(blackUserIds);

        ArrayList<BlackListVo> blackListVos = new ArrayList<>();
        BlackListVo blackListVo = null;
        for (UserInfo userInfo : userInfoList) {
            blackListVo = new BlackListVo();
            blackListVo.setAge(userInfo.getAge());
            blackListVo.setAvatar(userInfo.getLogo());
            blackListVo.setGender(userInfo.getSex().name().toLowerCase());
            blackListVo.setId(userInfo.getUserId());
            blackListVo.setNickname(userInfo.getNickName());

            blackListVos.add(blackListVo);
        }
        pageResult.setItems(blackListVos);
        return pageResult;
    }

    /**
     * 移除黑名单
     *
     * @param blackId
     * @return
     */
    public Boolean delBlacklistSerivce(Long blackId) {
        User user = UserThreadLocal.get();

        return this.blackListService.delBlacklist(user.getId(), blackId);
    }

    /**
     * 陌生人问题
     *
     * @param content
     */
    public void saveQuestions(String content) {
        User user = UserThreadLocal.get();
        this.questionService.save(user.getId(), content);
    }

    /**
     * 通知查询
     *
     * @return
     */
    public SettingsVo querySettings() {
        User user = UserThreadLocal.get();
        SettingsVo settingsVo = new SettingsVo();
        //设置用户基本信息
        settingsVo.setId(user.getId());
        settingsVo.setPhone(user.getMobile());

        //查询用户的配置数据
        Settings settings = this.questionService.querySettings(user.getId());
        if (ObjectUtil.isNotEmpty(settings)) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }

        //查询陌生人问题

        settingsVo.setStrangerQuestion(this.tanHuaService.queryQuestion(user.getId()));
        return settingsVo;

    }

    public void updateNotification(Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {

    }
}
